import time
import math
import numpy as np
import matplotlib.pyplot as plt

def calc_vis_field(fov_deg, dip_deg, landing_deg, dist):
    fov_rad = np.deg2rad(fov_deg/2)
    dip_rad = np.deg2rad(dip_deg)
    landing_rad = np.deg2rad(landing_deg)
    assert fov_rad < dip_rad

    h = dist*np.sin(landing_rad)
    center_rad = math.pi/2 - dip_rad
    near_rad = center_rad - fov_rad
    far_rad = center_rad + fov_rad
    vertical_near_point = h*np.tan(center_rad)-h*np.tan(near_rad)
    vertical_far_point = h*np.tan(far_rad) -h*np.tan(center_rad)

    flight_vis_pos = -dist*np.cos(landing_rad)+ h*np.tan(center_rad)

    return [flight_vis_pos-vertical_near_point, flight_vis_pos+vertical_far_point]


if __name__ == '__main__':
    angle_landing = np.rad2deg(np.arcsin(0.1))
    angle_dip = 7.2
    angle_fov = 12.84

    res = []
    for d in np.arange(5000, 5001, 100):
        # print(d,
        #     calc_vis_field(
        #     fov_deg=angle_fov,
        #     dip_deg=angle_dip,
        #     landing_deg=angle_landing,
        #     dist=d)
        #     )
        f = calc_vis_field(
            fov_deg=angle_fov,
            dip_deg=angle_dip,
            landing_deg=angle_landing,
            dist=d)
        res.append([d, *f])
    res = np.array(res)
    plt.plot(res[:,0], res[:,1], label='near')
    plt.plot(res[:,0], res[:,2], label='far')
    plt.legend()
    plt.show()
    print(time.strftime('%H:%M:%S', time.localtime()))
